package com.dhcc.sds.base.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.dhcc.cdc.sys.dao.CdcProductMapper;
import com.dhcc.cdc.sys.dao.DictionaryMapper;
import com.dhcc.cdc.sys.dao.DictypeMapper;
import com.dhcc.cdc.sys.entity.CdcProductEntity;
import com.dhcc.cdc.sys.entity.ConfigurationEntity;
import com.dhcc.cdc.sys.entity.DictionaryEntity;
import com.dhcc.cdc.sys.entity.DictypeEntity;
import com.dhcc.cdc.sys.service.IConfigurationService;
import com.dhcc.core.framework.base.service.impl.BaseServiceImpl;
import com.dhcc.core.framework.result.node.ZTreeNode;
import com.dhcc.core.framework.util.CommonUtil;
import com.dhcc.core.framework.util.DateUtil;
import com.dhcc.sds.base.dao.SdsQcentityMapper;
import com.dhcc.sds.base.entity.SdsQcentityEntity;
import com.dhcc.sds.base.service.ISdsCommonService;
import com.dhcc.sds.base.service.ISdsQcentityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 质控病种字典表数据服务层接口
 *
 * @ClassName: SdsQcentityServiceImpl
 * @Description: TODO
 * @author sunyingjie
 * @date 2021-03-22 10:10:52
 */
@Service("sdsQcentityService")
public class SdsQcentityServiceImpl extends BaseServiceImpl<SdsQcentityMapper, SdsQcentityEntity> implements ISdsQcentityService {
	@Autowired
	private CdcProductMapper productMapper;
	@Autowired
	private DictypeMapper dictypeMapper;
	@Autowired
	private DictionaryMapper dictionaryMapper;
	@Autowired
	private IConfigurationService configurationService;
	@Autowired
	private ISdsCommonService sdsCommonService;

	@Override
	public List<SdsQcentityEntity> page(Page<SdsQcentityEntity> page, Map<String, Object> map) {
		return this.baseMapper.page(page, map);
	}
	@Override
	public List<SdsQcentityEntity> list(Map<String, Object> map) {
		return this.baseMapper.list(map);
	}
	@Override
	public List<SdsQcentityEntity> listBtDesc() {
		return this.baseMapper.listBtDesc();
	}





	@Override
	public List<ZTreeNode> getEntityTree() {
		List<ZTreeNode> tree = this.baseMapper.tree();
//		tree.add(ZTreeNode.createRoot());
		return tree;
	}

	@Override
	public List<ZTreeNode> getEntityTreeByTypeCode(String typeCode) {
		Long entityTypeId = sdsCommonService.getDictId("QcentityType", typeCode);
		Map<String, Object> map = new HashMap<>();
		map.put("entityTypeId",entityTypeId);
		map.put("typeCode", typeCode);
		List<ZTreeNode> tree = this.baseMapper.treeByType(map);
		return tree;
	}



	@Override
	public JSONObject getQcentity() {


		JSONObject jsObj = new JSONObject();
		jsObj.put("","");
		List<SdsQcentityEntity> sdsQcentityEntities = this.baseMapper.selectList(null);
		for ( SdsQcentityEntity sdsQcentityEntity : sdsQcentityEntities){
			jsObj.put(sdsQcentityEntity.getId().toString(), sdsQcentityEntity.getBtDesc());
		}
		return jsObj;
	}
	@Override
	@Transactional(rollbackFor = Exception.class)
	public void importEntities(List<List<Object>> all) {
		for (int i = 1; i < all.size(); i++) {
			List<Object> ls = all.get(i);
			String abbrev = ls.get(0).toString();
			String btDesc = ls.get(1).toString();
			String url = ls.get(2).toString();
			Date pubDate = null;
			if (CommonUtil.isNotEmpty(ls.get(3))) {
				pubDate = DateUtil.parseDate(ls.get(3).toString());
			}
			BigDecimal cost = null;
			if (CommonUtil.isNotEmpty(ls.get(4))) {
				cost = BigDecimal.valueOf(Long.parseLong(ls.get(4).toString()));
			}
			Integer inDays = null;
			if (CommonUtil.isNotEmpty(ls.get(5))) {
				inDays = Integer.valueOf(ls.get(5).toString());
			}
			String operKey = ls.get(6).toString();
			Integer isActive = CommonUtil.equals(ls.get(7),"是")?1:0;
			Integer indNo = null;
			if (CommonUtil.isNotEmpty(ls.get(8))) {
				indNo = Integer.valueOf(ls.get(8).toString());
			}

			// 看看有没有病种系统
			String entityCatId = null;
			String entityCatDesc = ls.get(9).toString();
			if (CommonUtil.isNotEmpty(entityCatDesc)) {
				CdcProductEntity productEntity = new CdcProductEntity();
				productEntity.setCode("SDS");
				productEntity = productMapper.selectOne(productEntity);
				DictypeEntity dicType = new DictypeEntity();
				dicType.setProductId(productEntity.getId());
				dicType.setCode("EntityCat");
				dicType = dictypeMapper.selectOne(dicType);
				Map<String,Object> map = new HashMap<String,Object>();
				map.put("dictypeId", dicType.getId());
				map.put("description", entityCatDesc);
				List<DictionaryEntity> dicList =  dictionaryMapper.list(map);
				if (dicList.size()!=0) {
					entityCatId = String.valueOf(dicList.get(0).getId());
				}
			}

			// 判断病种是否重复
			SdsQcentityEntity entity = new SdsQcentityEntity();
			entity.setAbbrev(abbrev);
			entity = this.baseMapper.selectOne(entity);
			if (entity  == null) {
				entity = new SdsQcentityEntity();
				entity.setAbbrev(abbrev);
				entity.setBtDesc(btDesc);
				entity.setEntityCatId(entityCatId);
				entity.setUrl(url);
				entity.setCost(cost);
				entity.setInDays(inDays);
				entity.setPubDate(pubDate);
				entity.setOperKey(operKey);
				entity.setIndNo(indNo);
				entity.setIsActive(isActive);

				entity.insert();
			}else{
				entity.setAbbrev(abbrev);
				entity.setBtDesc(btDesc);
				entity.setEntityCatId(entityCatId);
				entity.setUrl(url);
				entity.setCost(cost);
				entity.setInDays(inDays);
				entity.setPubDate(pubDate);
				entity.setOperKey(operKey);
				entity.setIndNo(indNo);
				entity.setIsActive(isActive);

				entity.updateAllColumnById();
			}
		}
	}

	@Override
	public JSONArray getUpEntityMap() {
		Long entityTypeId = sdsCommonService.getDictId("QcentityType", "Up");
		Wrapper<SdsQcentityEntity> wrapper = new EntityWrapper<>();
		wrapper.eq("entity_type_id",entityTypeId);
		List<SdsQcentityEntity> sdsQcentityEntities = this.baseMapper.selectList(wrapper);
		JSONArray officeArry = new JSONArray();
		JSONObject obj = new JSONObject();
		obj.put("value", "");
		obj.put("text","全部");
		officeArry.add(obj);
		for(SdsQcentityEntity qcentityEntity : sdsQcentityEntities){
			JSONObject officeObj = new JSONObject();
			officeObj.put("value", qcentityEntity.getId() + "");
			officeObj.put("text", qcentityEntity.getBtDesc());
			officeArry.add(officeObj);
		}
		return officeArry;
	}

	@Override
	public JSONArray getEntityMapByTypeCode(String typeCode) {
		Long entityTypeId = sdsCommonService.getDictId("QcentityType", typeCode);
		Wrapper<SdsQcentityEntity> wrapper = new EntityWrapper<>();
		wrapper.eq("entity_type_id",entityTypeId);
		List<SdsQcentityEntity> sdsQcentityEntities = this.baseMapper.selectList(wrapper);
		JSONArray officeArry = new JSONArray();
		JSONObject obj = new JSONObject();
		obj.put("value", "");
		obj.put("text","全部");
		officeArry.add(obj);
		for(SdsQcentityEntity qcentityEntity : sdsQcentityEntities){
			JSONObject officeObj = new JSONObject();
			officeObj.put("value", qcentityEntity.getId() + "");
			officeObj.put("text", qcentityEntity.getBtDesc());
			officeArry.add(officeObj);
		}
		return officeArry;
	}

	@Override
	public JSONArray getEntityMapByConfig(String productCode, String code) {
		JSONArray officeArry = new JSONArray();
		JSONObject obj = new JSONObject();
		obj.put("value", "");
		obj.put("text","全部");
		officeArry.add(obj);
		ConfigurationEntity config = configurationService.selectByProductCodeAndCode(productCode, code);
		if (!StringUtils.isEmpty(config)){
			String value = config.getValue();
			if (!StringUtils.isEmpty(value)){
				if (value.contains(",")){
					String[] split = value.split(",");
					for (String s : split){
						SdsQcentityEntity sdsQcentityEntity = new SdsQcentityEntity();
						sdsQcentityEntity.setAbbrev(s);
						SdsQcentityEntity sdsQcentityEntity1 = this.baseMapper.selectOne(sdsQcentityEntity);
						if (!StringUtils.isEmpty(sdsQcentityEntity1)){
							JSONObject officeObj = new JSONObject();
							officeObj.put("value", sdsQcentityEntity1.getId() + "");
							officeObj.put("text", sdsQcentityEntity1.getBtDesc());
							officeArry.add(officeObj);
						}
					}
				}else {
					SdsQcentityEntity sdsQcentityEntity = new SdsQcentityEntity();
					sdsQcentityEntity.setAbbrev(value);
					SdsQcentityEntity sdsQcentityEntity1 = this.baseMapper.selectOne(sdsQcentityEntity);
					if (!StringUtils.isEmpty(sdsQcentityEntity1)){
						JSONObject officeObj = new JSONObject();
						officeObj.put("value", sdsQcentityEntity1.getId() + "");
						officeObj.put("text", sdsQcentityEntity1.getBtDesc());
						officeArry.add(officeObj);
					}
				}
			}
		}
		return officeArry;
	}

	@Override
	public JSONArray getEntityMap2() {
		List<SdsQcentityEntity> sdsQcentityEntities = this.baseMapper.selectList(null);
		JSONArray officeArry = new JSONArray();
		for(SdsQcentityEntity qcentityEntity : sdsQcentityEntities){
			JSONObject officeObj = new JSONObject();
			officeObj.put("value", qcentityEntity.getId() + "");
			officeObj.put("text", qcentityEntity.getBtDesc());
			officeArry.add(officeObj);
		}
		return officeArry;
	}
}